package com.soft.collateral.controller;

import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.constant.RedisImgConstant;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.FileStrategyService;
import com.soft.collateral.service.impl.FileUploadContext;
import com.soft.collateral.service.impl.QiNiuYunFileServiceImpl;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/7/25
 */
@RestController
@RequestMapping("/admin/file")
@Api(tags = "后台系统-文件上传接口")
@RequiredArgsConstructor
public class FileController {

    private final FileStrategyService fileStrategyService;
    private final RedisTemplate<String,Object> redisTemplate;
    private final QiNiuYunFileServiceImpl qiNiuYunFileServiceImpl;

    @PostMapping("upload")
    public Result upload(MultipartFile file){
        String url = fileStrategyService.uploadFile(file);
        if(StringUtils.hasLength(url)){
            //图片上传至服务器
//            redisTemplate.opsForSet().add(RedisImgConstant.UPLOAD_PIC , url);
            return new Result(true , MessageConstant.PIC_UPLOAD_SUCCESS , url);
        }
        return new Result(false , MessageConstant.PIC_UPLOAD_FAIL);
    }

    @PostMapping("delete")
    public Result delete(String url){
        boolean flag = fileStrategyService.deleteFile(url);
        if(flag){
            return new Result(true , MessageConstant.PIC_DELETE_SUCCESS);
        }
        return new Result(false , MessageConstant.PIC_DELETE_FAIL);
    }

//    @PostMapping("upload")
//    public Result upload(MultipartFile file){
//
//        String url = qiNiuYunFileServiceImpl.uploadFile(file);
//        if(StringUtils.hasLength(url)){
//            return new Result(true , MessageConstant.PIC_UPLOAD_SUCCESS , url);
//        }
//        return new Result(false , MessageConstant.PIC_UPLOAD_FAIL);
//    }
//
//    @PostMapping("delete")
//    public Result delete( String url){
//
//        boolean flag = qiNiuYunFileServiceImpl.deleteFile(url);
//        if(flag){
//            return new Result(true , MessageConstant.PIC_DELETE_SUCCESS);
//        }
//        return new Result(false , MessageConstant.PIC_DELETE_FAIL);
//    }
}
